Skip to content

Adds pytest_markdown_docs_markdown_it for custom markdownit parser #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2025

Conversation

thomasjpfan
Copy link
Contributor

Issue: Fixes #31

This PR adds a pytest_markdown_docs_markdown_it pytest hook to allow developers to customize the markdown_it parser. This means they can add plugins such as admon_plugin to the parser.

An alternative is to add something like https://github.com/modal-labs/pytest-markdown-docs/blob/main/src/pytest_markdown_docs/_runners.py, but using a pytest hook feels a little more natural with how pytest does things. For example, pytest-xdist has uses a pytest_xdist_make_scheduler to configure a custom scheduler:

https://github.com/pytest-dev/pytest-xdist/blob/f83299cd646965ac0560adddcf7204adde9f486e/src/xdist/newhooks.py#L96-L100

@freider
Copy link
Collaborator

freider commented Apr 8, 2025

Thanks for the contribution! I like the pytest hook pattern for this, since the markdown flavor is probably going to be the same across a codebase anyways.

@@ -123,15 +124,20 @@ def get_prefixed_strings(


def extract_fence_tests(
markdown_it_parsers: typing.List["MarkdownIt"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something here, but why is this a list? (seems like we are throwing away anything except the first element anyway)

Copy link
Contributor Author

@thomasjpfan thomasjpfan Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pytest returns a list because the hook can be defined multiple times. For example:

tests/
    conftest.py <- hook can be defined here
    io/
        conftest.py <- same hook can be defined here
        my_test.py

The default is to have a list that contains all the hooks and the nested conftest.py gets called first so they are first in the hooks list.


I simplified the code by using @pytest.hookspec(firstresult=True), which returns the first one by default: 0be6a10

Also, 5fe7f6f, which defines a default MarkdownIt parser using the hook.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, yeah this makes sense! Thanks for the explanation and I like the firstresult and default implementation

@@ -294,8 +300,13 @@ def find_object_tests_recursive(
or "<Unnamed obj>"
)
fence_syntax = FenceSyntax(self.config.option.markdowndocs_syntax)
markdown_it_parsers = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding the list comment above - maybe it's better to validate the hook return value here (making sure it's at most one specification, since that's what we support) and then just pass in the single MarkdownIt we want to use?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With 0be6a10 and 5fe7f6f, this should be a single MarkdownIt object.

The remaining user error is returning a non-MarkdownIt object:

def pytest_markdown_docs_markdown_it():
    return "abc"

Do you think it's worth checking for this edge case?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the other changes I don't think we need to check for anything here 👍

Copy link
Collaborator

@freider freider left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, thanks!

@freider freider merged commit c932861 into modal-labs:main Apr 9, 2025
@freider freider mentioned this pull request Apr 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Not compatible with mkdocs's admonition
2 participants